<?php
/**
 * 《加油记录》实现类
 *
 * @author 孙晓晔
 * @version $Id: Maintain.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/CarUtil.class.php');

class Oil extends CarUtil {
	public $tbl = Constant::tbl_car_oil;

	public $status = array(
		'-' => '请选择',
		'1' => '申请加油',
		'2' => '等待加油',
		'3' => '已经加油',
		
		'9' => '其它',	
	);

	public $orderby = array(
		'-' => '请选择',
		'car' => '车辆',
		'date' => '日期',
		'quantity' => '油量',
		'cost' => '费用',
		'km1' => '公里数',
		'emp' => '负责人',	
		
		'1' => '创建时间',
		'2' => '更新时间',
	);

	public $groupby = array(
		'-' => '请选择',
		'car' => '车辆',
		'date' => '日期',
		'emp' => '负责人',
	
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);

	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);
		
		// $controller->assign('date', strtotime("-1 YEAR"));
		
		$controller->assign('orderby_s', "date");

		$controller->display($request, 'car/oil/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == 'mobile') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['mobile'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$car_s = $this->_input($request['car_s']);

		$name_s = $request['name_s'];
		
		$date1_s = $request['date1_s'];
		$date2_s = $request['date2_s'];

		$where = " WHERE 1=1";

		if (!empty($car_s)) {
			$where .= " AND car = '" . $car_s . "'";
		}
		
		if (!empty($date1_s)) {
			$where .= " AND date >= '" . $date1_s . "'";
		}
		
		if (!empty($date2_s)) {
			$where .= " AND date < '" . $date2_s . "'";
		}
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);				

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'car/oil/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function my($controller, $request) {

		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'car/oil/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);
		
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$car = $this->_input($request['car']);
		$date = $request['date'];
		
		$name = addslashes($request['name']);
		$content = addslashes($request['content']);

		$quantity = $request['quantity'];
		$cost = $request['cost'];
		$km1 = $request['km1'];

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, emp, car, date, name, km1, quantity, cost, content, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $car, $date, $name, $km1, $quantity, $cost, $content, $_SESSION['id']);
		// debug($sql);
		$db->query($sql);
		
		$request['id_s'] = $db->insert_id();

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function _car($id, $maintain) {
		if ($maintain == '3') {
			return false;
		}

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET status = '%s' WHERE id = '%s'";
		$sql = sprintf($fmt, Constant::tbl_car, '2', $id);
		// debug($sql);
		$db->query($sql);

		clean(Constant::tbl_car . "_id_" . $id);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'car/oil/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$car = $this->_input($request['car']);
		$date = $request['date'];
		
		$name = addslashes($request['name']);
		$content = addslashes($request['content']);

		$quantity = $request['quantity'];
		$cost = $request['cost'];
		$km1 = $request['km1'];
		$km2 = $request['km2'];
		$km = intval($km2) - intval($km1);

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', emp = '%s', date = '%s', car = '%s', name = '%s', quantity = '%s', ";
		$fmt .= "cost = '%s', km1 = '%s', km2 = '%s', km = '%s', content = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $date, $car, $name, $quantity, $cost, $km1, $km2, $km, $content, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}
		
		$this->_km2($car, $date, $km1);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _km2($car, $date, $km1) {
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE car = '%s' AND date < '%s' ORDER BY date DESC LIMIT 1";
		$sql = sprintf($fmt, $this->tbl, $car, $date);
		debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			$km = intval($km1) - intval($row['km1']);
			
			$fmt = "UPDATE %s SET km2 = '%s', km = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $km1, $km, $row['id']);
			debug($sql);
			$db->query($sql);
		}
	}
	
	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'car/oil/copy');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'car/oil/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['car_t'] = Name::car($row['car']);
		$row['wear'] = $this->_wear($row['km'], $row['quantity']);
		
		$row = $this->_view_t($row);

		return $row;
	}
	
	function _wear($km, $quantity) {
		if ($km <= 0) {
			return 0;
		}
		
		$ret = 100 * $quantity / $km;

		return number_format($ret, 2, '.', '');
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function group($controller, $request) {
		$this->pretreat_group($controller);

		// $controller->assign('t1', strtotime('-1 month'));
		// $controller->assign('t2', strtotime('+1 day'));

		$num = array(
			'1' => '按次数',
			'2' => '按金额',
		);
		$controller->assign('num_option', $num);

		$controller->display($request, 'car/maintain/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'emp');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	function groupby_dd($px) {
		if ($px == '1') {
			return "DATE_FORMAT(t1, '%Y-%m-%d %H')";
		}

		if ($px == '2') {
			return "DATE_FORMAT(t1, '%Y-%m-%d')";
		}

		if ($px == '3') {
			return "DATE_FORMAT(t1, '%Y-%m')";
		}

		return $px;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function groupPost($controller, $request) {
		$this->pretreat($controller);
			
		$this->groupPara($controller, $request);

		$num_s = $request['num_s'];
		if (!empty($num_s) && $num_s == '2') {
			$fmt = "SELECT %s AS a, SUM(cost) AS c FROM %s a ";
		} else {
			$fmt = "SELECT %s AS a, COUNT(cost) AS c FROM %s a ";
		}
		$sql = sprintf($fmt, $this->s, $this->tbl);

		$where = $this->_where($request);

		$request['sql'] = sprintf("%s %s GROUP BY %s ORDER BY %s %s", $sql, $where, $this->s, $this->orderby, $this->desc);
		
		$this->_group($controller, $request);

		// $controller->display($request, 'list');
		return true;
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'emp') {
			$row['a'] = sprintf("%s [%s]", Name::emp($row['a']), $row['a']);
		} elseif ($px == 'car') {
			$row['a'] = Name::car($row['a']);
		} elseif ($px == 'maintain') {
			$row['a'] = $this->maintain[$row['a']];
		} else {
		
			$row = $this->_name_t($row, $px);
		}
		
		return $row;
	}
}
?>